// export function set(data) {
//     var json_data =
//         localStorage.setItem("userinfo", data);
//     return
var KEY_storage_prefix = "financial_admin_";

// 同步设置数据
export function setStorageSync(key, val) {
  var key = KEY_storage_prefix + key;
  val = JSON.stringify({ data: val, type: typeof val });
  localStorage.setItem(key, val);
}

// 同步获取数据
export function getStorageSync(key, def = false) {
  var key = KEY_storage_prefix + key;
  var val = localStorage.getItem(key);
  val = JSON.parse(val);
  if (val) {
    return val["data"];
  } else {
    return def;
  }
}

// 根据Key删除指定数据
export function removeItem(key) {
  var key = KEY_storage_prefix + key;
  localStorage.removeItem(key);
  return true;
}

// 删除所有localStorage中的项
export function clear() {
  localStorage.clear();
  return true;
}

export function usageOfLocalStorage() {
  var storageSize = 0;
  for (var item in localStorage) {
    storageSize += localStorage.getItem(item).length;
  }
  return storageSize / 1024.0; //返回localstorage使用空间大小，单位为KB
}
